package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQuery;
import cn.wolfcode.luowowo.comment.repository.ScenicCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    private ScenicCommentRepository repository;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ScenicComment save(ScenicComment comment) {
        if (comment.getId() == null || "".equals(comment.getId().trim())){
            comment.setCreateTime(new Date());
        }
        repository.save(comment);
        return comment;
    }

    @Override
    public Page query(ScenicCommentQuery qo) {
        // 拼接条件
        Query query = new Query();
        if (qo.getScenicId() != -1) {
            query.addCriteria(Criteria.where("scenicId").is(qo.getScenicId()));
        }
        // 查询总条数
        long total = mongoTemplate.count(query, ScenicComment.class);
        // 如果总条数为0，返回空集合
        if (total == 0) {
            return Page.empty();
        }
        // 查询分页的结果集
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "createTime");
        // 查当前显示数据
        List<ScenicComment> list = mongoTemplate.find(query, ScenicComment.class);
        return new PageImpl(list, pageable, total);
    }

    @Override
    public ScenicComment findById(String id) {

        return repository.findById(id).get();
    }


    @Override
    public void commentThumbUp(String toid, Long userId) {
        ScenicComment comment = repository.findById(toid).get();
        List<Long> thumbuplist = comment.getThumbuplist();
        // 1:当请求过来时,需要判断点赞用户id集合中是否有当前登录用户id
        if (!thumbuplist.contains(userId)) {
            // 2:如果当前登录用户id不存在集合中,表示点赞操作,点赞数 + 1,将用户id添加到集合中
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            thumbuplist.add(userId);
        }else{
            // 3:如果当前登录用户id存在集合中,表示取消点赞操作,点赞数 - 1,将用户id从集合中移除
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            thumbuplist.remove(userId);
        }
        repository.save(comment);
    }

    @Override
    public List<ScenicComment> queryListByUserId(Long uid) {
        // 构建限制条件
        Criteria criteria = Criteria.where("userId").is(uid);
        // 创建查询对象
        Query query = new Query();
        // 添加限制条件
        query.addCriteria(criteria);

        return mongoTemplate.find(query, ScenicComment.class);
    }

    @Override
    public Long getCount(Long scenicId) {
        Query query = new Query();
        if (scenicId != -1) {
            query.addCriteria(Criteria.where("scenicId").is(scenicId));
        }
        long count = mongoTemplate.count(query, ScenicComment.class);
        return count;
    }
}
